from Case.python_ui.Common.basepage import Basepage
import pytest, time
from Case.python_ui.Pageobject.user_product_waf import UserProductWaf
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_product_cms_locator import ProductCmsLocator
from Case.python_ui.PageLocate.user_product_waf_locator import ProductWafLocator
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.Test_data.login_data import Logindata
from selenium import webdriver
from Case.python_ui.PageLocate.user_product_eni_locator import ProductEniLocator
from selenium.common.exceptions import NoSuchElementException


class UserProductEni(Basepage):

    # 判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False, '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    # 点击左上角菜单按钮进入菜单页面
    def enter_menu_page(self):
        doc = '点击左上角菜单按钮进入菜单页面'
        self.wait_elevisble(ProductEniLocator.product_my_resources_tittle, doc)
        self.move_mouse_to_click_element(ProductEniLocator.product_menu_button, doc)
        time.sleep(3)
        self.wait_elevisble(ProductEniLocator.product_menu_page, doc)

    # 点击菜单ECS按钮进入Elastic Compute Service控制台页面
    def enter_eni_ecs_page(self):
        doc = '点击菜单ECS按钮进入Elastic Compute Service控制台页面'
        time.sleep(6)
        for i in range(3):
            try:
                self.scroll_bar_operation(ProductEniLocator.ecs_menu_button)
                self.wait_elevisble(ProductEniLocator.ecs_menu_button, doc)
                time.sleep(2)
                self.move_mouse_to_click_element(ProductEniLocator.ecs_menu_button, doc)
                break
            except:
                self.refresh_page()
                time.sleep(3)
                self.enter_menu_page()
                continue
        time.sleep(8)
        # 检测ecs控制台页面信息
        self.driver.switch_to.frame(self.get_element(ProductEniLocator.into_iframe, doc))
        self.wait_elevisble(ProductEniLocator.ecs_overview_title, doc)
        self.save_screen(doc)

    # 点击ECS页面的左侧ENI标题按钮进入ENI控制台页面
    def enter_eni_page(self):
        doc = '点击ECS页面的左侧ENI标题按钮进入ENI控制台页面'
        time.sleep(2)
        self.move_mouse_to_click_element(ProductEniLocator.eni_title_btn, doc)
        time.sleep(2)
        self.save_screen(doc)
        self.wait_elevisble(ProductEniLocator.eni_page_title, doc)

    # 点击ENI页面的Create ENI按钮创建ENI
    def create_eni(self):
        doc = '点击ENI页面的Create ENI按钮创建ENI'
        self.move_mouse_to_click_element(ProductEniLocator.create_eni_btn, doc)
        time.sleep(8)
        self.wait_elevisble(ProductEniLocator.create_eni_page, doc)
        # ENI name 输入框输入名称
        self.send_keys(ProductEniLocator.create_eni_name_input, 'eni001', doc)
        time.sleep(2)
        self.move_mouse_to_click_element(ProductEniLocator.create_eni_submit_btn, doc)
        time.sleep(2)
        self.save_screen(doc)
        self.wait_elevisble(ProductEniLocator.create_eni_successful_tips, doc)
        self.check_attribute(self.get_element(ProductEniLocator.create_eni_successful_tips, doc).text, 'Operation succeeded')
        self.move_mouse_to_click_element(ProductEniLocator.create_eni_tips_ok_btn, doc)
        time.sleep(3)
        self.wait_elevisble(ProductEniLocator.create_eni_console_page, doc)
        self.save_screen(doc)
        self.check_attribute(self.get_element(ProductEniLocator.create_eni_name, doc).text, 'eni001')

    # 点击ENI页面的列表modify修改ENI名称
    def modify_eni_name(self):
        doc = '点击ENI页面的列表modify修改ENI名称'
        time.sleep(1)
        self.move_mouse_to_click_element(ProductEniLocator.eni_modify_btn, doc)
        time.sleep(2)
        self.save_screen(doc)
        self.keyboard_event(ProductEniLocator.eni_modify_name_input, doc)
        time.sleep(2)
        self.send_keys(ProductEniLocator.eni_modify_name_input, 'modifyeni000', doc)
        self.save_screen(doc)
        time.sleep(3)
        # 点击OK按钮保存修改
        self.click_element(ProductEniLocator.eni_modify_ok_btn, doc)
        time.sleep(4)
        self.wait_elevisble(ProductEniLocator.eni_modify_new_name, doc)
        self.check_attribute(self.get_element(ProductEniLocator.eni_modify_new_name, doc).text, 'modifyeni000')

    # 点击ENI页面的列表Bind绑定实例按钮
    def eni_bind_instance(self):
        doc = '点击ENI页面的列表Bind绑定实例按钮'
        time.sleep(1)
        self.move_mouse_to_click_element(ProductEniLocator.eni_bind_instance_btn, doc)
        time.sleep(2)
        self.click_element(ProductEniLocator.eni_bind_instance_box, doc)
        time.sleep(2)
        self.save_screen(doc)
        self.move_mouse_to_click_element(ProductEniLocator.eni_bind_instance_data_btn, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(ProductEniLocator.bind_eni_tips_ok_btn, doc)

    # 点击ENI页面的列表delete按钮删除eni
    def delete_eni(self):
        doc = '点击ENI页面的列表delete按钮删除eni'
        time.sleep(1)
        self.move_mouse_to_click_element(ProductEniLocator.eni_more_btn, doc)
        self.move_mouse_to_click_element(ProductEniLocator.eni_delete_btn, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductEniLocator.eni_delete_tips_box, doc).text, 'Are you sure that you want to delete the selected ENI?')
        self.move_mouse_to_click_element(ProductEniLocator.eni_delete_tips_ok_btn, doc)

    # ENI页面搜索功能
    def eni_search_func(self):
        doc = 'ENI页面搜索功能'
        time.sleep(1)
        # 点击搜索类型下拉框
        self.move_mouse_to_click_element(ProductEniLocator.eni_search_drop_btn, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(ProductEniLocator.eni_search_name_type_btn, doc).text, 'ENI Name')
        self.check_attribute(self.get_element(ProductEniLocator.eni_search_id_type_btn, doc).text, 'ENI ID')
        self.check_attribute(self.get_element(ProductEniLocator.eni_search_ip_type_btn, doc).text, 'Private IP Address')
        self.check_attribute(self.get_element(ProductEniLocator.eni_search_instance_type_btn, doc).text, 'Instance ID')
        self.check_attribute(self.get_element(ProductEniLocator.eni_search_tag_type_btn, doc).text, 'Tag')
        # 点击搜索框进行搜索操作
        self.move_mouse_to_click_element(ProductEniLocator.eni_search_input_btn, doc)
        self.send_keys(ProductEniLocator.eni_search_input_btn, 'eni001', doc)
        time.sleep(1)
        self.move_mouse_to_click_element(ProductEniLocator.eni_search_btn, doc)
        time.sleep(3)
        self.check_attribute(self.get_element(ProductEniLocator.eni_search_data_name, doc).text, 'eni001')



